package com.example.myapplication42

import android.app.Activity
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AlertDialog
const val SCORE = "SCORE"
const val REQUEST_CODE_MODIFY = 1
const val REQUEST_CODE_GAME = 2
const val REQUEST_CODE_GAME2 = 3  // 添加新的请求码

class MainActivity : AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        val modify = findViewById<android.widget.Button>(R.id.button_modify)
        modify.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivityForResult(intent, REQUEST_CODE_MODIFY)
        }
        val game = findViewById<android.widget.Button>(R.id.button_game)
        game.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)
            startActivityForResult(intent, REQUEST_CODE_GAME)
        }
        val game3 = findViewById<android.widget.Button>(R.id.button_new)
        game3.setOnClickListener {
            val intent = Intent(this, FourthActivity::class.java)
            startActivityForResult(intent, REQUEST_CODE_GAME2)
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        when (requestCode) {
            REQUEST_CODE_MODIFY -> {
                if (resultCode == Activity.RESULT_OK) {
                    // 处理来自 SecondActivity 的结果
                    val name = data?.getStringExtra("name")
                    val xuehao = data?.getStringExtra("xuehao")
                    val phone = data?.getStringExtra("phone")
                    val address = data?.getStringExtra("address")
                    val gender = data?.getStringExtra("gender")
                    val hobbies = data?.getStringExtra("hobbies")
                    val imageByteArray = data?.getByteArrayExtra("image")
                    // 更新 UI
                    findViewById<TextView>(R.id.textView3).text = "姓名: $name"
                    findViewById<TextView>(R.id.textView5).text = "学号: $xuehao"
                    findViewById<TextView>(R.id.textView8).text = "电话: $phone"
                    findViewById<TextView>(R.id.textView9).text = "地址: $address"
                    findViewById<TextView>(R.id.textView4).text = "性别: $gender"
                    findViewById<TextView>(R.id.textView6).text = "爱好: $hobbies"
                    findViewById<TextView>(R.id.textView2).text = "$name+$xuehao"

                    imageByteArray?.let {
                        val bitmap = BitmapFactory.decodeByteArray(it, 0, it.size)
                        findViewById<ImageView>(R.id.imageView).setImageBitmap(bitmap)
                    }
                }
            }
            REQUEST_CODE_GAME -> {
                if (resultCode == Activity.RESULT_OK) {
                    // 处理来自 ThirdActivity 的结果
                    val score = data?.getStringExtra(SCORE)
                    val textView: TextView = findViewById(R.id.score)
                    textView.text = score
                }
            }
            REQUEST_CODE_GAME2 -> {
                // 处理来自 FourthActivity 的结果
                if (resultCode == Activity.RESULT_OK) {
                    val score = data?.getStringExtra(SCORE)
                    val textView: TextView = findViewById(R.id.score)
                    textView.text = "分数: $score"
                }
            }
        }
    }
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.menu_about -> {
                showAboutDialog()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
    private fun showAboutDialog() {
        AlertDialog.Builder(this)
            .setTitle("关于")
            .setMessage("Card Game 应用\n版本: 1.0\n作者: xinn")
            .setPositiveButton("确定", null)
            .show()
    }

}